package com.cy.interceptor;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.io.PrintWriter;

/**
 * @author 小李
 * @version 1.0
 * @date 2021/9/7 14:46
 */
@Component
public class ServiceBlockExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       BlockException e) throws Exception {
        //设置响应数据的编码
        response.setContentType("application/json;charset=utf-8");
        //告诉客户端要响应的数据类型以及客户端以什么编码呈现数据
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        out.println("{\"code\":\"601\",\"msg\":\"访问太频繁,稍等片刻再访问\"}");
        out.flush();

    }
}
